import java.net.*;
import java.io.*;

public class UDPTalker implements Runnable {
	
	int bsize;
	int port;
	InetAddress host;
	int packets;
	String contents;


	public UDPTalker(int port, String host, int packets, String contents, int bsize){
		try {
			this.host = InetAddress.getByName(host);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		this.port = port;
		this.packets = packets;
		this.contents = contents;
		this.bsize = bsize;
	}

	@Override
	public void run() {
		DatagramSocket ss = null;
		
		try {
			//new socket
			ss = new DatagramSocket();
			
			//stuff message into a byte array
			byte[] msg = new byte[bsize];
			
			
			for(int i = 0; i < packets; i++) {
				String psh = contents + String.valueOf(i);
				msg = psh.getBytes();
				
				System.out.println(psh);
				
				DatagramPacket outDgp = new DatagramPacket(msg, msg.length, host, port);
				ss.send(outDgp);
				
				
			}
			
			ss.close();
			ss = null;
			return;
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}
}